import {onMounted, reactive, ref} from 'vue';
import { queryIpAge } from "@api/url/erp/qualityAssurance";
import { formatDate } from "@utils/time"
import router from '@/router';
import {quxiao} from "@api/url/erp/baseMessage";
import notification from "@arco-design/web-vue/es/notification";
export default function importConfig() {
  // 开始时间
  const startDate = new Date();
  startDate.setDate(1);
  // 结束时间
  const endDate = new Date();
  const base = reactive<any>({
    checkForm: {
        article: "",    // 品号
        checky: "过程检验",     // 检验项目 必传['调机首检'，'首检','来料检验','过程检验','成品检验','检验详情列表']
        code: "",       // TY001+'-'+TY002+'-'+TY003+'-'+TY009+'-'+TY019
        codeid: "",     // 员工编号 必传
        current: 1,     // 页码 必传
        endTime: formatDate(endDate),    // 结束时间
        gy: "",         // 工艺
        mv002: "",      // 设备编码
        romm: "",       // 场别
        size: 10,        // 页数大小 必传
        startTime: formatDate(startDate),  // 开始时间 必传
        status: "",     // 状态条件
        te017: "",       // 品号
        timeInterval: [formatDate(startDate), formatDate(endDate)],    // 时间区间
    },
    loadingData: false,
    pagination: {
        defaultPageSize: 10,
        total: 1, //总条数
        current: 1,
        pageSize: 10,
        showTotal: true,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 40, 50],
    },
    tableData: []
  });

  onMounted(() => {
    base.checkForm.codeid = localStorage.getItem('userId');
    queryIpAgeFun();
  })

  // 选择时间
  const onChange = (val) => {
    base.checkForm.startTime = val[0].replaceAll('-','');
    base.checkForm.endTime = val[1].replaceAll('-','');
  }


  // 查询
  const selectDataListFun = () => {
    base.checkForm.current = base.pagination.current;
    base.checkForm.size = base.pagination.pageSize;
    queryIpAgeFun();
  }

  // 获取首检的数据方法
  const queryIpAgeFun = () => {
    base.checkForm.startTime = base.checkForm.startTime.replaceAll('-','');
    base.checkForm.endTime = base.checkForm.endTime.replaceAll('-','');
    base.loadingData = true;
    queryIpAge(base.checkForm).then((res: any) => {
        console.log(res);
        if(res.code === 200){
            base.tableData = res.data.data;
            base.pagination.total = res.data.total;
            base.loadingData = false;
        }
    })
  }

  //
  const getTableData = () => {
    selectDataListFun();
  }


  /**
   * @exports viewDetails 查看详情单
   * @param param 品号
   */
  const viewDetails = (param) => {
    console.log(base.checkForm.timeInterval);
    router.push({
      path: '/inspectionPolicy',
      query: {
        article: param.pinhao,
        code: param.singetsu.trim() + '-' + param.odd.trim() + '-' + param.shijian.trim() + '-' + param.sbm.trim() + '-' + param.gx.trim(),
        time: JSON.stringify(base.checkForm.timeInterval),
        gy: param.gy,
        checky: '过程检验',
      },
    });
  };

    //取消检验
    const cancelInspection=(param)=>{
        let code=param.singetsu.trim() + '-' + param.odd.trim() + '-' + param.shijian.trim() + '-' + param.sbm.trim() + '-' + param.gx.trim();
        quxiao(code).then(res=>{
            notification.info(res.message);
            if(res.code==200){
                selectDataListFun();
            }
        });
    };

  // 重置
  const resetData = () => {
    base.checkForm.article = '';
    base.checkForm.code = '';
    base.checkForm.gy = '';
    base.checkForm.mv002 = '';
    selectDataListFun();
  }

  return {
    base,
    onChange,
    selectDataListFun,
    getTableData,
    viewDetails,
    resetData,
      cancelInspection,
  };
}
